package com.sm.healthy.ui.view.ecg;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.sm.healthy.R;

import java.util.List;


/**
 *
 */
public class EcgView2 extends View {


    private int XPoint = 0; // X label start point
    private int YPoint = 1000; // Y label start point
    private int XLength = 720;
    private int YLength = 480;
    private float bgScale = YLength / 50; // scale for background
    private int MaxDataSize = 1000;
    private float xStep;

    private float[] data = new float[MaxDataSize];
    private int count;
    private double gain = 1.0;
    private Paint paint;

    public EcgView2(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        count = 0;
    }

    private void initView() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(lineSize);
    }

    int lineSize = 2;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.TRANSPARENT);
        XLength = getWidth();
        YPoint = (int) (getHeight());
        YLength = YPoint;
        bgScale = (float) (YLength / 35.0); // scale for background

        /**
         * draw ECG data lines
         **/
        xStep = (float) XLength / MaxDataSize;
        float xStart = XLength - count * xStep;
        float yStep = (float) YLength / 5;
        paint.setColor(getResources().getColor(R.color.black_line));
        paint.setStrokeWidth(lineSize);

        for (int i = 1; i < count; i++) {
            canvas.drawLine(xStart + XPoint + (i - 1) * xStep,
                    (float) (YPoint - data[i - 1] * yStep - 15 * bgScale),
                    xStart + XPoint + i * xStep,
                    (float) (YPoint - data[i] * yStep - 15 * bgScale),
                    paint);
        }
    }

    public void addData(int newData, int heartRate) {
        if (count == MaxDataSize) {
            for (int i = 0; i < data.length - 1; i++) {
                data[i] = data[i + 1];
            }
            data[count - 1] = newData;
        } else {
            data[count] = newData;
            count++;
        }
    }

    public void setData(List<Integer> xx) {
        for (int i = 0; i < xx.size(); i++) {
            int curr;
            if(xx.get(i)==null){
                curr = 0;
            }else{
                curr = xx.get(i);
            }
            this.data[i] = (float) (gain * 18.3 * curr / (128 * 1000));
        }
        count = xx.size();
    }

    public void setData(int[] data) {
        for (int i = 0; i < data.length; i++) {
            this.data[i] = (float) (gain * 18.3 * data[i] / (128 * 1000));
        }
        count = data.length;
    }

    public void clear() {
        for (int i = 0; i < data.length; i++) {
            data[i] = 0;
        }
        count = 0;
    }

    public void setGain(double gain) {
        this.gain = gain;
    }

    public int getMaxDataSize() {
        return MaxDataSize;
    }

    public int getCount() {
        return count;
    }

    public void setMaxDataSize(int size){
        this.MaxDataSize = size;
    }


}
